package com.simplegui.api.util;

import com.simplegui.api.FrameRateListener;

public abstract class FrameRatePerSecondCounter implements FrameRateListener, Runnable{

	private int frames;
	private int framesPerSecond;
	
	public FrameRatePerSecondCounter() {
		new Thread(this).start();
	}
	
	@Override
	public void onDraw() {
		frames++;
	}

	@Override
	public void run() {
		while(true){
			try {
				Thread.sleep(1000);
				framesPerSecond = frames;
				frames = 0;
				framesPerSecondChanged();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	protected abstract void framesPerSecondChanged();

	public int getFramesPerSecond() {
		return framesPerSecond;
	}

}
